package java67_adv9;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ReceiveFile {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("下载哪个文件?");
		String name = input.next();
		try {
			// localhost 127.0.0.1
			// Socket s = new Socket("192.168.1.146", 9999);
			// Socket s = new Socket("localhost", 9999);
			Socket s = new Socket("47.93.151.29", 9999);
			InputStream in = s.getInputStream();

			OutputStream out = s.getOutputStream();
			PrintWriter pw = new PrintWriter(out, true);
			pw.println(name);
			FileOutputStream fout = new FileOutputStream("e:/" + name);
			int ch;
			while ((ch = in.read()) != -1) {
				fout.write(ch);
			}
			fout.flush();
			fout.close();
			s.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
